package com.cgbl.service.ui

import android.os.Bundle
import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.TeamMemberAdapter
import com.cgbl.service.databinding.AppActivityBalanceProjectBinding
import com.cgbl.service.pop.FilterPop
import com.cgbl.service.vm.BalanceProjectViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.MemberAdapterBean

@Route(path = AppConstants.Router.Project.A_BALANCE_PROJECT)
class BalanceProjectActivity :
    GLBaseActivity<AppActivityBalanceProjectBinding, BalanceProjectViewModel>() {

    private var id = ""

    lateinit var teamMemberAdapter: TeamMemberAdapter

    lateinit var filterPop: FilterPop

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_balance_project

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        teamMemberAdapter = TeamMemberAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = teamMemberAdapter
        }
        teamMemberAdapter.setNewInstance(mutableListOf(MemberAdapterBean()))
        teamMemberAdapter.setOnItemClickListener { _, _, _ ->
            val filterPop = FilterPop(viewModel.teamMemberIds, this@BalanceProjectActivity) {
                it.add(MemberAdapterBean())
                teamMemberAdapter.setNewInstance(it)
                viewModel.teamMemberIds.clear()
                teamMemberAdapter.data.forEach { member ->
                    if (!TextUtils.isEmpty(member.id)) {
                        viewModel.teamMemberIds.add(member.id ?: "")
                    }
                }
            }
            XPopup.Builder(this@BalanceProjectActivity).asCustom(filterPop).show()
        }
        viewModel.fetchTeamProfile(id)
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView?.text = "添加团队"
        backView?.setOnClickListener { finish() }
    }

    override fun initViewObservable() {
        viewModel.uc.teamMemberEvent.observe(this) {
            it.add(MemberAdapterBean())
            teamMemberAdapter.setNewInstance(it)
            viewModel.teamMemberIds.clear()
            teamMemberAdapter.data.forEach { member ->
                if (!TextUtils.isEmpty(member.id)) {
                    viewModel.teamMemberIds.add(member.id ?: "")
                }
            }
        }
    }

    override fun initParam() {
        id = intent.getStringExtra(AppConstants.BundleKey.ID) ?: ""
    }
}